import {
  ajaxFun
} from "../../../utils/ajax";
Page({

  data: {
    active: 0,
    pageNumber:1,
    status:"",
    orderList:[]
  },
onLoad(){
  this.getOrderList(this.data.status)
},

async  getOrderList(status){
   let result=await ajaxFun("/order","get",{
    pageNumber:this.data.pageNumber,
    status
   })   
   if(result.resultCode===200){
    //  console.log(result.data.list.length);
     if(result.data.list.length==0){
        wx.showToast({
          title: '我是有底线的',
          icon:"none"
        })
      return false
     }
     this.setData({
       orderList:[...this.data.orderList,...result.data.list]
     })
   }
},
onReachBottom(){//下拉分页
  this.setData({
    pageNumber:this.data.pageNumber+1
  },()=>{
    this.getOrderList(this.data.status)
  })
},
onChange(e) {
  //  console.log(e.detail.index);
  let status=e.detail.index==0?"":e.detail.index-1;
  console.log(status);
  this.setData({
    status,
    pageNumber:1,
    orderList:[]
  },()=>{
    this.getOrderList(this.data.status)
  })
  
},
cancelOrder(e){//取消订单
  console.log(e.target.id);
  wx.showModal({
    title: '提示',
    content: '是否取消订单',
    complete:async (res) => {
      if (res.confirm) {
        let result=await ajaxFun(`/order/${e.target.id}/cancel`,"put")
        this.setData({
          pageNumber:1,
          orderList:[]
        },()=>{
          this.getOrderList(this.data.status)
        })
      }
    }
  })
},
payfor(e){//去支付
  console.log();
  const index=e.target.dataset.index
  let obj=this.data.orderList[index];
  console.log(obj);
  let item=JSON.stringify(obj)
  wx.navigateTo({
    url: `/pages/payfor/payfor?item=${item}`,
  })

},
async finished(e){
  console.log(e.target.id);
  let result=await ajaxFun(`/order/${e.target.id}/finish`,"put");
  if(result.resultCode==200){
     this.setData({
      active: 5,//前端tab的样式
      pageNumber:1,
      status:4,//给后台字段的值
      orderList:[]
     },()=>{
      this.getOrderList(this.data.status)
     })
    wx.showToast({
      title: '收货成功',
      icon:"none"
    })
  }
}
})